const gulp=require('gulp')
const uglify=require('gulp-uglify')
const babel=require('gulp-babel')
const rev=require('gulp-rev')
const clean=require('gulp-clean')
const minify=require('gulp-minify-css')
const imagemin=require('gulp-imagemin')
const htmlmin=require('gulp-htmlmin')
const collector=require('gulp-rev-collector')
const fs=require('fs')
const webserver=require('gulp-webserver')

gulp .task('clean',over=>{
    if (fs.existsSync('dist/')) {
        gulp.src('dist/',{read:true})
            .pipe(clean())
    }
    setTimeout(()=>{
        over()
    },2000)
})
gulp.task('js',over=>{
    gulp.src('./src/js/*.js')
    .pipe(babel({"presets": ["env"]}))
    .pipe(rev())
    .pipe(uglify())
    .pipe(gulp.dest('./dist/js/'))
    .pipe(rev.manifest())
	.pipe(gulp.dest('./temp/js'))
    over()
})
gulp.task('css',over=>{
    gulp.src('./src/css/*.css')
    .pipe(rev())
    .pipe(minify())
    .pipe(gulp.dest('./dist/css/'))
    .pipe(rev.manifest())
	.pipe(gulp.dest('./temp/css'))
    over()
})
gulp.task('imgs',over=>{
    gulp.src('./src/imgs/*.*')
    .pipe(imagemin())
    .pipe(gulp.dest('./dist/imgs/'))
    setTimeout(()=>{
        over()
    },20000)
})
gulp.task('html',()=>{
    var options = {
        collapseWhitespace:true,
        collapseBooleanAttributes:true,
        removeComments:true,
        removeEmptyAttributes:true,
        removeScriptTypeAttributes:true,
        removeStyleLinkTypeAttributes:true,
        minifyJS:true,
        minifyCSS:true
        };
    // gulp.src('./src/*.html')
    gulp.src(['temp/**/*.json','./src/*.html'])
    .pipe(collector())
    .pipe(htmlmin(options))
    .pipe(gulp.dest('./dist/'))
})
gulp.task('build',gulp.series('clean','js','css','imgs','html'),()=>{
    console.log('打包完成');
})
gulp.task('server',()=>{
		gulp.src('./src')
        .pipe(webserver({
			port: 80,//端口号
			livereload: true,//是否热更新
			open: true,//是否打开浏览器
		}))
})